<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version = "1.0"
                xmlns:Atom="http://www.w3.org/2005/Atom"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
                xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
                xmlns:doap="http://usefulinc.com/ns/doap#"
                xmlns:asfext="http://projects.apache.org/ns/asfext#"
                >

  <xsl:output method="xml" indent="yes" />
  <xsl:param name="location" />
  <xsl:param name="projectName" />

  <xsl:template match="/">
    <xsl:apply-templates select="rdf:RDF|Atom:feed" />
  </xsl:template>

  <xsl:template match="rdf:RDF">
    <xsl:apply-templates select="doap:Project"/>
  </xsl:template>

  <xsl:template match="Atom:feed">
    <xsl:apply-templates select="Atom:entry/Atom:content/doap:Project" />
  </xsl:template>

  <xsl:template match="doap:Project">
      <xsl:call-template name="project" />
  </xsl:template>

  <xsl:template name="project">
<document>
  <xsl:call-template name="header" />
  <xsl:call-template name="body" />
</document>
  </xsl:template>

  <xsl:template name="header">
  <properties>
    <title>Project Details for <xsl:value-of select="doap:name"/></title>
    <link rel="meta" title="DOAP" type="application/rdf+xml">
      <xsl:attribute name="href">
        <xsl:value-of select="$location"/>
      </xsl:attribute>
    </link>
    <link rel="alternate" title="RSS" type="application/rss+xml">
      <xsl:attribute name="href">
        <xsl:text>http://projects.apache.org/feeds/rss/</xsl:text>
        <xsl:value-of select="$projectName"/>
        <xsl:text>.xml</xsl:text>
      </xsl:attribute>
    </link>
    <link rel="alternate" title="Atom" type="application/atom+xml">
      <xsl:attribute name="href">
        <xsl:text>http://projects.apache.org/feeds/atom/</xsl:text>
        <xsl:value-of select="$projectName"/>
        <xsl:text>.xml</xsl:text>
      </xsl:attribute>
    </link>
    <index>
      <section>projects</section>
    </index>
  </properties>
  </xsl:template>

  <xsl:template name="body">
  <body>
    <section>
      <title>
        <xsl:value-of select="doap:name"/>
      </title>
      <xsl:call-template name="addLinks"/>
      <xsl:call-template name="project-summary"/>
      <xsl:call-template name="project-releases" />
      <xsl:call-template name="project-standards" />
    </section>
  </body>
  </xsl:template>

  <xsl:template match="@rdf:resource">
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="."/>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </a>
  </xsl:template>
    
  <xsl:template match="doap:programming-language">
    <a>
      <xsl:attribute name="href">
        <xsl:text>/indexes/language.html#</xsl:text>
        <xsl:value-of select="."/>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </a>
    <xsl:if test="not(position() = last())">
      <xsl:text>, </xsl:text>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="doap:category">
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="./@rdf:resource" />
      </xsl:attribute>
      <xsl:attribute name="href">
        <xsl:text>/indexes/category.html#</xsl:text>
        <xsl:value-of select="substring-after(@rdf:resource, 'category/')" />
      </xsl:attribute>
      <xsl:value-of select="substring-after(@rdf:resource, 'category/')"/>
    </a>
    <xsl:if test="not(position() = last())">
      <xsl:text>, </xsl:text>
    </xsl:if>
  </xsl:template>


  <xsl:template match="asfext:pmc">
    <xsl:choose>
      <xsl:when test="contains(@rdf:resource, '.rdf')">
        <xsl:call-template name="pmcLink">
          <xsl:with-param name="url">
            <xsl:value-of select="@rdf:resource"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="pmcLink">
          <xsl:with-param name="url">
            <xsl:text>http://asylum.zones.apache.org/projects/</xsl:text>
            <xsl:value-of select="substring-before(substring-after(@rdf:resource, 'http://'), '.')"/>
            <xsl:text>.rdf</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="pmcLink">
    <xsl:param name="url"/>
    <a>
      <xsl:attribute name="href">
        <xsl:text>/indexes/pmc.html#</xsl:text>
        <xsl:value-of select="document($url)/rdf:RDF/asfext:PMC/@rdf:about"/>
      </xsl:attribute>
      <xsl:value-of select="document($url)/rdf:RDF/asfext:PMC/asfext:name"/>
    </a>
  </xsl:template>

  <xsl:template name="project-description">
    <xsl:choose>
     <xsl:when test="doap:description">
      <p>
        <xsl:value-of select="doap:description"/>
      </p>
     </xsl:when>
     <xsl:otherwise>
       <xsl:if test="doap:shortdesc">
         <p>
           <xsl:value-of select="doap:shortdesc"/>
         </p>
       </xsl:if>
     </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="project-homepage">
    <xsl:if test="doap:homepage">
    <tr>
      <td>Website</td>
      <td><xsl:value-of select="doap:homepage"/></td>
    </tr>
    </xsl:if>
  </xsl:template>

  <xsl:template name="project-summary">

   <xsl:call-template name="project-description" />

   <table>
    <tr>
      <td>Programming Languages</td>
      <td>
        <xsl:apply-templates select="doap:programming-language" />
      </td>
    </tr>

    <tr>
      <td>Categories</td>
      <td>
        <xsl:apply-templates select="doap:category" />
      </td>
    </tr>

    <xsl:if test="doap:mailing-list">
    <tr>
      <td class="left">Mailing Lists</td>
      <td class="right">
        <xsl:apply-templates select="doap:mailing-list/@*" />
      </td>
    </tr>
    </xsl:if>

    <xsl:if test="doap:bug-database">
    <tr>
      <td class="left">Bug/Issue Tracker</td>
      <td class="right">
        <xsl:apply-templates select="doap:bug-database/@*" />
      </td>
    </tr>
    </xsl:if>

    <xsl:if test="doap:wiki">
      <tr>
        <td class="left">Wiki</td>
        <td class="right">
          <xsl:apply-templates select="doap:wiki/@*" />
        </td>
      </tr>
    </xsl:if>

    <tr>
      <td class="left">License</td>
      <td class="right">
        <xsl:choose>
          <xsl:when test="doap:license/@rdf:resource = 'http://usefulinc.com/doap/licenses/asl20'">
            <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License Version 2.0</a>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="doap:license/@*" />
          </xsl:otherwise>
        </xsl:choose>
      </td>
    </tr>              

    <tr>
      <td class="left">Project Website</td>
      <td class="right">
        <xsl:apply-templates select="doap:homepage/@*" />
      </td>
    </tr>

    <xsl:if test="asfext:pmc">
    <tr>
      <td class="left">PMC</td>
      <td class="right">
        <xsl:apply-templates select="asfext:pmc"/>
      </td>
    </tr>
    </xsl:if>

  </table>

  </xsl:template>

  <xsl:template match="asfext:mailing-list/asfext:Mail-list">

  <tr>
    <td class="title">
      <xsl:apply-templates select="doap:name"/>
    </td>
    <td class="right"><xsl:value-of select="doap:description"/></td>
    <td class="right">
      <a>
        <xsl:attribute name="href">
          <xsl:choose>
            <xsl:when test="asfext:subscribe">
              <xsl:value-of select="asfext:subscribe/@rdf:resource"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>mailto:</xsl:text>
              <xsl:value-of select="substring-before(doap:name,'@')"/>
              <xsl:text>-subscribe@</xsl:text>
              <xsl:value-of select="substring-after(doap:name,'@')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
        Subscribe
      </a>
    </td>
    
    <td class="right">
      <xsl:choose>
        <xsl:when test="asfext:archives">
          <a><xsl:attribute name="href">
          <xsl:value-of select="asfext:archives/@rdf:resource"/>
          </xsl:attribute>Archives</a>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>Not Archived</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </td>    
    </tr>
  </xsl:template>  

  <xsl:template name="project-scm">

    <div class="content">
      <xsl:choose>
        <xsl:when test="doap:repository">
          <xsl:for-each select="doap:repository/doap:SVNRepository">
            <table>
              <tr>
                <td class="left">Browse</td>
                <td class="right">
                  <xsl:apply-templates select="doap:browse/@*" />
                </td>
              </tr>
              <tr>
                <td class="left">SVN Direct</td>
                <td class="right">
                    <xsl:apply-templates select="doap:location/@*" />
                </td>
              </tr>              
            </table>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <p>No source control information provided.</p>
        </xsl:otherwise>
      </xsl:choose>
    </div>
  </xsl:template>

  <xsl:template match="doap:release/doap:Version">
    <tr>
            <td class="title">
                <xsl:value-of select="doap:name" />
            </td>
            <td class="right"><xsl:value-of  select="doap:revision" /></td>
            <td class="right"><xsl:value-of  select="doap:created" /></td>
    </tr>
  </xsl:template>
 
  <xsl:template name="project-releases">
    <section>
      <title>Project Release Information</title>

    <xsl:if test="doap:download-page">
    <div class="content">
      <p>Releases can be downloaded from
        <xsl:for-each select="doap:download-page">
          <xsl:apply-templates select="./@*" />
          <xsl:if test="not(position() = last())">
            <xsl:text> or </xsl:text>
          </xsl:if>
        </xsl:for-each>            
      </p>
    </div>
    </xsl:if>

    <xsl:choose>
      <xsl:when test="doap:release">
        <p>Most recent releases:</p>

        <div class="content">  
          <table>
           <tr>
             <th>Release</th>
             <th>Version</th>
             <th>Date</th>
           </tr>         
           <xsl:apply-templates select="doap:release/doap:Version" />   
          </table>
       </div>
      </xsl:when>
      <xsl:otherwise>
        <p>No current releases.</p>
      </xsl:otherwise>
    </xsl:choose>
   
   <p>Access to the source code:</p>
  
   <xsl:call-template name="project-scm" />

    </section>
  </xsl:template>

  <xsl:template match="asfext:standard|asfext:Standard">
    <li>
      <a>
        <xsl:attribute name="href">
          <xsl:value-of select="asfext:url/@rdf:resource"/>
        </xsl:attribute>
        <xsl:value-of select="asfext:title"/>
      </a>
      <br/>
      <div class="smallitalic">
        <xsl:value-of select="asfext:id"/>, published by <xsl:value-of select="asfext:body"/>
      </div>
    </li>
  </xsl:template>

  <xsl:template name="project-standards">
    <xsl:if test="count(asfext:implements) &gt; 0 or count(asfext:standard) &gt; 0">
      <section>
        <title>Relevant Standards</title>
        <div class="content">
          <ul>
            <xsl:apply-templates select="asfext:standard"/>
            <xsl:apply-templates select="asfext:implements/asfext:Standard"/>
          </ul>
        </div>
      </section>
    </xsl:if>
  </xsl:template>

  <xsl:template name="addLinks">
  <p>
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="$location" />
      </xsl:attribute>
      <img src="/images/doap.png" border="0" alt="DOAP File"/>
    </a>
    <xsl:text> </xsl:text>
    <a>
      <xsl:attribute name="href">
        <xsl:text>/feeds/rss/</xsl:text>
        <xsl:value-of select="$projectName" />
        <xsl:text>.xml</xsl:text>
      </xsl:attribute>
      <img src="/images/rss.jpg" border="0" alt="RSS File"/>
    </a>
    <xsl:text> </xsl:text>
    <a>
      <xsl:attribute name="href">
        <xsl:text>/feeds/atom/</xsl:text>
        <xsl:value-of select="$projectName" />
        <xsl:text>.xml</xsl:text>
      </xsl:attribute>
      <img src="/images/blue-icon.png" border="0" alt="Atom Feed"/>
    </a>
  </p>
  </xsl:template>

</xsl:stylesheet>
